﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using PlanningGIS.Util;
using PlanningGIS.Util.IO;
using PlanningGIS.Util.Log;
using PlanningGIS.Util.Data;
using PlanningGIS.Util.Drawing;
using PlanningGIS.Util.Security;
using PlanningGIS.Framework;
using PlanningGIS.ManageUtility;
using PlanningGIS.ManageUtility.DataAccess;
using PlanningGIS.ManageUtility.Modules;
using PlanningGIS.Win.Controls;
using PlanningGIS.Framework.Config;

namespace PlanningGIS.Entry
{
    /// <summary>
    /// 用户信息修改界面
    /// </summary>
    public partial class FrmUserInfo : DevComponents.DotNetBar.Office2007Form
    {
        /// <summary>
        /// 用户对象
        /// </summary>
        UserInfo u;
        /// <summary>
        /// 当前登录用户的ID
        /// </summary>
        private string LoginID;
        /// <summary>
        /// 当前登录名
        /// </summary>
        private string LoginName;
        /// <summary>
        /// 修改设置
        /// </summary>
        zjgisOperateType op = zjgisOperateType.Update;
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmUserInfo()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmUserInfo_Load(object sender, EventArgs e)
        {
            LoginID = FrameworkHelper.CurrentUser.ID;
            cmbDepartment.BindData();
            BindInfo(LoginID);
            ChangedControl(zjgisOperateType.Update);
            LoginName = FrameworkHelper.CurrentUser.Name;
        }

        /// <summary>
        /// 绑定用户信息
        /// </summary>
        /// <param name="id">用户ID</param>
        private void BindInfo(string id)
        {
            try
            {
                u = ManageHelper.DBContext.GetEntryByID<UserInfo>(id);
                if (u == null) return;
                txtLogin.Text = u.Login_Name;
                txtName.Text = u.Name;
                cmbDepartment.SetSelectValue(u.DepartMent_ID);
                txtOfficePhone.Text = u.OfficePhone;
                txtMobile.Text = u.MobilePhone;
                txtLogin.Tag = u.ID;
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(ex.ToString());
            }
        }

        /// <summary>
        /// 改变控件的状态
        /// </summary>
        /// <param name="op">1修改</param>
        private void ChangedControl(zjgisOperateType op)
        {
            txtLogin.ReadOnly = op == zjgisOperateType.UnKnow ? false : true;
            txtName.ReadOnly = op == zjgisOperateType.UnKnow ? true : false;
            cmbDepartment.Enabled = op == zjgisOperateType.UnKnow ? false : true;
            txtOfficePhone.ReadOnly = op == zjgisOperateType.UnKnow ? true : false;
            txtMobile.ReadOnly = op == zjgisOperateType.UnKnow ? true : false;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidUser()) return;
            if (MsgHelper.ShowYesOrNoQueryMsgBox("您确定要保存修改吗？") != DialogResult.Yes) return;
            Log log = new Log()
            {
                USER_ID = FrameworkHelper.CurrentUser.ID,
                USER_NAME = FrameworkHelper.CurrentUser.Name,
                OP_TYPE = PlanningGIS.ManageUtility.Modules.zjgisLogType.Modify,
                //DATA_INVOLOVE = string.Format("访问的数据为{0} {1}中的{2}", res.Server, res.Database, res.DataName),
                NOTES = "申请访问数据，但由于数据原因，导致数据访问失败",
                OP_STATUS = zjgisLogStatus.Fail,
            };

            u.Name = txtName.Text.Trim();
            u.MobilePhone = txtMobile.Text.Trim();
            u.OfficePhone = txtOfficePhone.Text.Trim();
            u.DepartMent_ID = cmbDepartment.SelectedDepartmentID;
            u.ID = LoginID;
            
            this.Cursor = Cursors.WaitCursor;
            try
            {
                int rst = 0;
                if (string.IsNullOrWhiteSpace(txtLogin.Tag.ToString())) return;
                u.ID = LoginID;
                rst = ManageHelper.DBContext.Update<UserInfo>(u, "id", txtLogin.Tag.ToString(), false);
                //Log.WriteLog(LoginName, Log.zjgisLogType.ModifyUser, Log.zjgisLogStatus.Success, u.Name);
                if (rst > 0)
                {
                    MsgHelper.ShowInfomationMsgbox("操作成功");
                    FrameworkHelper.CurrentUser.Name = u.Name;
                    RefreshData(u.ID, op);
                }
                else
                {
                    MsgHelper.ShowErrorMsgbox("操作失败");
                }
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(ex.ToString());
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// 验证输入的值是否有效
        /// </summary>
        /// <returns></returns>
        private bool ValidUser()
        {
            if (string.IsNullOrWhiteSpace(txtName.Text))
            {
                MsgHelper.ShowErrorMsgbox("姓名不能为空");
                txtName.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(txtLogin.Text))
            {
                MsgHelper.ShowErrorMsgbox("登录名称不能为空");
                txtLogin.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 刷新信息
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="opp">操作类型</param>
        private void RefreshData(string id, zjgisOperateType opp)
        {
            BindInfo(id);
            if (u == null) return;
            FrameworkHelper.MdiForm.Refresh();
        }
    }
}